#include <cubefs/api/api.hpp>
#include <natives.h>
#include <decls.hpp>
#include <exceptions.hpp>

#include <cubefs/core/tools/types/dispatcher.hpp>

#define CUBEFS_SOURCE "list.hpp:" CUBEFS_AT

using namespace cubefs::api;
using namespace cubefs::tools;

using namespace std;



JNIEXPORT jint JNICALL LIST_PKG(List, size) (JNIEnv *env, jclass, jlong handle) {
    CUBEFS_EXC_BEGIN

    List* list = Handle2Ptr<List>(handle);
    return list->size();

    CUBEFS_EXC_HANDLE
    return 0;
}

JNIEXPORT void JNICALL LIST_PKG(List, Remove) (JNIEnv *env, jclass, jlong handle, jint index) {
    CUBEFS_EXC_BEGIN

    List* list = Handle2Ptr<List>(handle);
    list->Remove(index);

    CUBEFS_EXC_HANDLE
}

JNIEXPORT void JNICALL LIST_PKG(List, RemoveAll) (JNIEnv *env, jclass, jlong handle, jint from, jint to) {
    CUBEFS_EXC_BEGIN

    List* list = Handle2Ptr<List>(handle);
    list->RemoveAll(from, to);

    CUBEFS_EXC_HANDLE
}

JNIEXPORT void JNICALL LIST_PKG(List, Clear) (JNIEnv *env, jclass, jlong handle) {
    CUBEFS_EXC_BEGIN

    List* list = Handle2Ptr<List>(handle);
    list->Clear();

    CUBEFS_EXC_HANDLE
}

JNIEXPORT jint JNICALL LIST_PKG(List, GetElementSize) (JNIEnv *env, jclass, jlong handle) {
    CUBEFS_EXC_BEGIN

    List* list = Handle2Ptr<List>(handle);
    return list->GetElementSize();

    CUBEFS_EXC_HANDLE
    return 0;
}


JNIEXPORT jlong JNICALL LIST_PKG(List, GetElement__JI) (JNIEnv *env, jclass, jlong handle, jint idx) {
    CUBEFS_EXC_BEGIN

    List* list = Handle2Ptr<List>(handle);
    return Ptr2Handle(list->GetElement(idx));

    CUBEFS_EXC_HANDLE
    return 0;
}

JNIEXPORT void JNICALL LIST_PKG(List, GetElement__JIJ) (JNIEnv *env, jclass, jlong handle, jint idx, jlong element_handle) {
    CUBEFS_EXC_BEGIN

    List* list 		= Handle2Ptr<List>(handle);
    Value* element 	= Handle2Ptr<Value>(element_handle);

    list->GetElement(idx, element);

    CUBEFS_EXC_HANDLE
}

JNIEXPORT void JNICALL LIST_PKG(List, SetElement) (JNIEnv *env, jclass, jlong handle, jint idx, jlong element_handle) {
    CUBEFS_EXC_BEGIN

    List* list 		= Handle2Ptr<List>(handle);
    const Value* element 	= Handle2Ptr<Value>(element_handle);

    list->SetElement(idx, element);

    CUBEFS_EXC_HANDLE
}

JNIEXPORT void JNICALL LIST_PKG(List, Insert) (JNIEnv *env, jclass, jlong handle, jint idx, jlong element_handle) {
    CUBEFS_EXC_BEGIN

    List* list 		= Handle2Ptr<List>(handle);
    const Value* element 	= Handle2Ptr<Value>(element_handle);

    list->Insert(idx, element);

    CUBEFS_EXC_HANDLE
}

JNIEXPORT void JNICALL LIST_PKG(List, Add) (JNIEnv *env, jclass, jlong handle, jlong element_handle) {
    CUBEFS_EXC_BEGIN

    List* list 		= Handle2Ptr<List>(handle);
    const Value* element 	= Handle2Ptr<Value>(element_handle);

    list->Add(element);

    CUBEFS_EXC_HANDLE
}

JNIEXPORT jlong JNICALL LIST_PKG(List, CreateValueHolder) (JNIEnv *env, jclass, jlong handle) {
    CUBEFS_EXC_BEGIN

    List* list 		= Handle2Ptr<List>(handle);

    return Ptr2Handle(list->CreateValueHolder());

    CUBEFS_EXC_HANDLE
    return 0;
}

JNIEXPORT jboolean JNICALL LIST_PKG(List, IsElementPrimitive) (JNIEnv *env, jclass, jlong handle) {
    CUBEFS_EXC_BEGIN

    List* list 		= Handle2Ptr<List>(handle);

    return list->IsElementPrimitive();

    CUBEFS_EXC_HANDLE
    return false;
}

#undef CUBEFS_SOURCE
